package com.itheima.mybatiscurd.mapper;

import com.itheima.mybatiscurd.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * EmpMapper
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/13 10:07
 */
@Mapper
public interface EmpMapper {
    /**
     * 根据ID删除员工信息 预编译SQL
     *
     * @param id
     */
    @Delete("delete from emp where id = #{id}")
    void deleteById(Integer id);

    /**
     * 新增员工
     * 1. 对于高版本的Mybatis可以直接用 实体的属性取值（高版本兼容低版本）
     * 2. 对于低版本的Mybatis可以用 @Param指定参数名取值（企业中）
     *
     * @param emp
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into emp(username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time)    " +
            "values (#{username}, #{password}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, " +
            " #{deptId}, #{createTime}, #{updateTime})")
    Integer insert(Emp emp);

    /**
     * 根据ID更新
     *
     * @param emp
     */
    @Update("update emp " +
            "set username    =#{username   }, " +
            "    password    =#{password   }, " +
            "    name        =#{name       }, " +
            "    gender      =#{gender     }, " +
            "    image       =#{image      }, " +
            "    job         =#{job        }, " +
            "    entrydate   =#{entrydate  }, " +
            "    dept_id     =#{deptId    }, " +
            "    update_time =#{updateTime} " +
            "where id = #{id}")
    void updateById(Emp emp);

    /**
     * 根据ID查询 员工 别名映射
     *
     * @param id
     * @return
     */
    @Select("select id,  " +
            "       username,  " +
            "       password,  " +
            "       name,  " +
            "       gender,  " +
            "       image,  " +
            "       job,  " +
            "       entrydate,  " +
            "       dept_id deptId,  " +
            "       create_time createTime,  " +
            "       update_time updateTime  " +
            "from emp  " +
            "where id = #{id}")
    Emp selectById(Integer id);

    /**
     * 根据ID映射，手动属性名映射
     *
     * @param id
     * @return
     */
    @Results(
            {
                    @Result(column = "dept_id", property = "deptId"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime")
            }
    )
    @Select("select id,  " +
            "       username,  " +
            "       password,  " +
            "       name,  " +
            "       gender,  " +
            "       image,  " +
            "       job,  " +
            "       entrydate,  " +
            "       dept_id ,  " +
            "       create_time ,  " +
            "       update_time   " +
            "from emp  " +
            "where id = #{id}")
    Emp selectById2(Integer id);

    /**
     * 根据ID查询， 开启驼峰命名映射
     *
     * @param id
     * @return
     */
    @Select("select * from emp where id = #{id}")
    Emp selectById3(Integer id);


    /**
     * 查名字中含 张 的员工(有SQL注入的风险)
     *
     * @param name
     * @return
     */
    @Select("select * from emp where name like '%${name}%'")
    List<Emp> selectListLikeName(String name);

    /**
     * 查名字中含 张 的员工 （推荐的写法）
     *
     * @param name
     * @return
     */
    @Select("select * from emp where name like concat('%', #{name}, '%')")
    List<Emp> selectListLikeName2(String name);

    /**
     * 通过XML编写查询语句
     *
     * @param id
     * @return
     */
    Emp selectById10(Integer id);

    List<Emp> selectList();

    void updateById10(Emp emp);

    List<Emp> selectList10(Emp emp);

    List<Emp> selectByIds(List<Integer> ids);
}
